
package com.sinux.generality.basesupport.utils.file;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultAttribute;
import org.springframework.util.StringUtils;
import org.xml.sax.SAXException;

public class XmlConf extends BaseConf{
    private static SAXReader reader;
    static{
        reader = new SAXReader();
        try {
            reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
    public XmlConf(String extension){
        try {
//            LogUtil.getLogger().info(resourcePath);
//            List<String> xmlList = SystemUtil.getFiles(resourcePath, this.getClass().getSimpleName().split("Conf")[0].toLowerCase());
            this.paramsMap = readXmls(readPath(extension), extension);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public Map<String,Map<String,Object>> readXmls(List<String> confList, String extension)throws Exception{
        Map<String,Map<String,Object>> map = new HashMap<String,Map<String,Object>>();
        for(String resourcePath:confList){
            map.put(getKeyword(resourcePath, extension), readXml(resourcePath));
        }
        return map;
    }
    public Map<String,Object> readXml(String resourcePath)throws Exception{
//        InputStream is = this.getClass().getResourceAsStream(resourcePath);
    	InputStreamReader is = null;
    	Map<String,Object> map = new HashMap<String,Object>();
    	try{
    		is = new InputStreamReader(new FileInputStream(resourcePath),"utf-8");
	        Document document = reader.read(is);
	        Element root = document.getRootElement();
	        childs(root, map);
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally{
    		if(is != null){
    			is.close();
    		}
    	}
    	return map;
    }
    public Map<String,Object> childs(Element element,Map<String,Object> map){
        List<Map<String,Object>> list = new LinkedList<Map<String,Object>>();
        List<Element> elementList = element.elements();
        String key = null;
        if(element.elements().size()>0){
            for(Element e:elementList){
                key = e.getName();
                Map<String,Object> nodeMap = new HashMap<String,Object>();
                List<DefaultAttribute> attributeList = e.attributes();
                //添加属性
                for(DefaultAttribute attr:attributeList){
                    nodeMap.put(attr.getName(), attr.getStringValue());
                }
                if(e.elements().size()>0){
                    childs(e,nodeMap);
                }else{
                	nodeMap.put("nodeValue",e.getStringValue());
                }
                list.add(nodeMap);
            }
        }
        if(!StringUtils.isEmpty(key)){
            map.put(key, list);
        }
        return map;
    }
}
